package com.oop.example.work.esport;

import com.oop.example.processing.OnLoadMerger;

/**
 *
 * @author -
 */
public class Esp2PortOnLoadMerger extends OnLoadMerger {

    private int lastSup = 0;

    @Override
    public boolean merge(String second) {
        boolean mergeCustomZone = mergeCustomZone(second);
        boolean mergeHomonym = mergeHomonym(second);
        if (result.isEmpty()) {
            result = second;
        }
        return mergeCustomZone || mergeHomonym;
    }

    private boolean mergeCustomZone(String second) {
        boolean merge = false;
        if (!second.contains("color:aqua")) {
            this.result += "<custom>" + second + "</custom>";
            merge = true;
        }
        return merge;
    }

    private boolean mergeHomonym(String second) {
        boolean merge = false;
        if (second.contains("<sup>")) {
            //int st = second.indexOf("<sup>") + "<sup>".length();
            //int end = second.indexOf("</sup>");
            //int current = Integer.valueOf(second.substring(st, end));
//lastSup
            if (result.contains("<sup>")) {
                //if (lastSup != 0 && lastSup == current - 1) {
                this.result += "<homonym/>" + second;
            } else {
                this.result = second;
            }
            merge = true;
        } else {
            lastSup = 0;
        }

        return merge;
    }

    public static void main(String[] args) {
        int coutnt = 10000;
        String second = "<p class=MsoNormal style='margin-bottom:0cm;margin-bottom:.0001pt;text-align: justify;line-height:normal;mso-pagination:none;mso-layout-grid-align:none; text-autospace:none'><b><span lang=EN-US style='font-size:14.0pt;mso-bidi-font-size: 7.5pt;font-family:\"Times New Roman\",\"serif\";mso-bidi-font-family:\"Times New Roman\"; mso-bidi-theme-font:minor-bidi;color:aqua;mso-ansi-language:EN-US'>a</span></b><b><sup><span lang=EN-US style='font-size:14.0pt;mso-bidi-font-size:4.0pt;font-family:\"Times New Roman\",\"serif\"; mso-bidi-font-family:\"Times New Roman\";mso-bidi-theme-font:minor-bidi; color:aqua;position:relative;top:-2.5pt;mso-text-raise:2.5pt;mso-ansi-language: EN-US'>2</span></sup></b><b><span lang=EN-US style='font-size:12.0pt; mso-bidi-font-size:4.0pt;font-family:\"Times New Roman\",\"serif\";mso-bidi-font-family: \"Times New Roman\";mso-bidi-theme-font:minor-bidi;color:black;mso-themecolor: text1;position:relative;top:-2.5pt;mso-text-raise:2.5pt;mso-ansi-language:EN-US'><span style='mso-spacerun:yes'> </span></span></b><span lang=EN-US style='font-size: 12.0pt;mso-bidi-font-size:7.5pt;font-family:\"Times New Roman\",\"serif\"; mso-bidi-font-family:\"Times New Roman\";mso-bidi-theme-font:minor-bidi; color:black;mso-themecolor:text1;mso-ansi-language:EN-US'>[a] <i>prep </i>A, para.<o:p></o:p></span></p>";
        for (int j = 0; j < 100; j++) {
            long start = System.nanoTime();
            for (int i = 0; i < coutnt; i++) {
                boolean res = second.matches("<sup>.*</sup>");
            }
            System.out.println("match    " + (System.nanoTime() - start));
            start = System.nanoTime();
            for (int i = 0; i < coutnt; i++) {
                boolean res = second.contains("<sup>");// && second.contains("</sup>");
            }
            System.out.println("contains " + (System.nanoTime() - start));
            System.out.println("-----------------------------------");
        }
    }
}
